package com.qbw.app.module.home.holder;

import android.content.Context;
import android.view.ViewGroup;

import com.http.okhttp.HttpTask;
import com.lib.util.ResUtil;
import com.qbw.app.R;
import com.qbw.app.databinding.HomeHolderWarningMessageBinding;
import com.qbw.app.model.Vehicle;
import com.qbw.app.model.VehicleHomeInfo;
import com.qbw.app.model.Warning;
import com.qbw.app.module.common.holder.CommonHolder;
import com.qbw.app.module.vehicle.VehicleDataBindingAdapter;
import com.qbw.app.module.vehicle.activity.VehicleWarningActivity;
import com.qbw.l.L;

import static com.qbw.app.request.impl.VehicleRequest.METHOD_HOME_INFO;

/**
 * @author xiansx
 * @date 2020/10/12
 */
public class HomeWaringMessageHolder extends CommonHolder<HomeWaringMessageHolder.Data, HomeHolderWarningMessageBinding> {

    public HomeWaringMessageHolder(Context context, ViewGroup viewParent) {
        super(context, R.layout.home_holder_warning_message, viewParent);
    }

    @Override
    public void bindData(int position, Data data) {
        super.bindData(position, data);
        updateWarningUi(mData.getHomeInfo().getAlarmInfo());
        itemView.setOnClickListener(v -> {
            Vehicle vehicle = mData.getHomeInfo().getVehicle();
            if (vehicle != null) {
                VehicleWarningActivity.startActivity(mContext, vehicle);
            }
        });
    }

    private void updateWarningUi(Warning.HomeInfo homeInfo) {
        if (homeInfo == null) {
            return;
        }
        if (homeInfo.getUnRead() <= 0) {
            mBinding.tvWarningMessage.setText(R.string.warning_message);
        } else if (homeInfo.getUnRead() == 1) {
            if (homeInfo.getAlarmInfo() != null) {
                mBinding.tvWarningMessage.setText(String.format(ResUtil.getString(R.string.warning_unread_message_one_format),
                                                                homeInfo.getAlarmInfo()
                                                                        .getAlarmName()));
            }
        } else {
            VehicleDataBindingAdapter.setWarningUnreadMany(mBinding.tvWarningMessage,
                                                           homeInfo.getUnRead());
        }
        mBinding.ivWarningMessage.setImageResource(homeInfo.getUnRead() <= 0 ? R.drawable.nm_icon_alarm : R.drawable.nm_icon_alarm_red);
    }

    public static class Data {
        private VehicleHomeInfo homeInfo;

        public Data(VehicleHomeInfo homeInfo) {
            this.homeInfo = homeInfo;
        }

        public VehicleHomeInfo getHomeInfo() {
            return homeInfo;
        }

        public void setHomeInfo(VehicleHomeInfo homeInfo) {
            this.homeInfo = homeInfo;
        }
    }

}
